package com.thiny.android.game.thunderbolt.role;

import com.thiny.android.game.thunderbolt.util.Constants;
import com.thiny.android.game.thunderbolt.view.GameView;

/**
 * 主角类
 * 
 * @author THINY
 * 
 * 
 *         飞机类型，决定了单发子弹类型： PLAYER_TYPE_1 普通单列 PLAYER_TYPE_2 双列 PLAYER_TYPE_3 三列
 * 
 *         子弹的大小杀伤力等，有子弹自身类型决定
 * 
 */
public class Player extends Plane {

	private int bulletType = Constants.PLAYER_BULLET_1;
	private int bulletSpeed=Constants.PLAYER_BULLET_SPEED;

	public Player(GameView gameView, float x, float y, int type, int speed,
			int life) {
		super(gameView, x, y, Constants.DIR_STOP, type, speed, life);
	}

	public void setBulletType(int bulletType) {
		this.bulletType = bulletType;		 
	}

	public void move(float offsetX, float offsetY) {
		if (!isAlive) {
			return;
		}
		
		x += offsetX;
		y += offsetY;
		
		//防止我方战机子弹出界
		if (x < mGameView.playerBullet2.getWidth()*5/2) {
			x = mGameView.playerBullet2.getWidth()*5/2;
		}
		if (x >= SCREEN_WIDTH - mGameView.playerBullet2.getWidth()*5/2) {
			x = SCREEN_WIDTH - mGameView.playerBullet2.getWidth()*5/2;
		}
		if (y < BITMAP_HEIGHT / 2) {
			y = BITMAP_HEIGHT / 2;
		}
		if (y >= SCREEN_HEIGHT - BITMAP_HEIGHT / 2) {
			y = SCREEN_HEIGHT - BITMAP_HEIGHT / 2;
		}
	}

	@Override
	public void setType(int type) {	 
		super.setType(type);
		setRoleBitmap(type);
	}

	@Override
	public void fire() {
		if (!isAlive) {
			return;
		}

		// 不同类型的战机，射出的子弹不一样
		int w = mGameView.getBitmapByBulletType(bulletType).getWidth();
		int h = mGameView.getBitmapByBulletType(bulletType).getHeight();
		switch (type) {
		case Constants.PLAYER_TYPE_1:
			mGameView.addPlayerBullet(new Bullet(mGameView, x, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			break;
		case Constants.PLAYER_TYPE_2:
			mGameView.addPlayerBullet(new Bullet(mGameView, x - w, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			mGameView.addPlayerBullet(new Bullet(mGameView, x + w, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			break;
		case Constants.PLAYER_TYPE_3:
			mGameView.addPlayerBullet(new Bullet(mGameView, x, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			mGameView.addPlayerBullet(new Bullet(mGameView, x - 2 * w, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			mGameView.addPlayerBullet(new Bullet(mGameView, x + 2 * w, y
					- (BITMAP_HEIGHT + h) / 2, Constants.DIR_UP, bulletType,
					bulletSpeed));
			break;

		}

	}

	//我方子弹提速
	public void bulletSpeedUp() {
		if (bulletSpeed<Constants.PLAYER_BULLET_SPEED_MAX) {
			bulletSpeed++;
		}
	}
	
	@Override
	public void move() {
	}

	@Override
	public void setRoleBitmap(int type) {
		switch (type) {
		case Constants.PLAYER_TYPE_1:
			mRoleBitmap = mGameView.player1;
			break;
		case Constants.PLAYER_TYPE_2:
			mRoleBitmap = mGameView.player2;
			break;
		case Constants.PLAYER_TYPE_3:
			mRoleBitmap = mGameView.player3;
			break;
		}
	}
	
	//使用道具时，增加生命值
	public void addLife() {
		if (life<Constants.LIFE_PLAYER_MAX) {
			life++;
		}
	}

	//减少生命值
	public void delLife() {
		life--;
	}
}
